<?php
namespace app\admin\validate\site\navi;

use think\Validate;

/**
 * 底部导航配置验证
 */
class NaviBottom extends Validate
{
    /**
     * 定义验证规则
     * 格式：'字段名'    =>    ['规则1','规则2'...]
     *
     * @var array
     */
    protected $rule = [
        'id'        => 'require',
        'title'     => 'require',
        'pid'       => 'require',
        'data_type' => 'require|in:0,1,2',
        'content'   => 'require|checkContent',
    ];

    /**
     * 定义错误信息
     * 格式：'字段名.规则名'    =>    '错误信息'
     *
     * @var array
     */
    protected $message = [
        'id'              => '操作出错',
        'title'           => '导航名称必须填写',
        'pid'             => '操作出错',
        'data_type'       => '导航类型错误',
        'content.require' => '导航内容不能为空',
    ];

    /**
     * 验证场景
     */
    protected $scene = [
        'create_group' => ['title'],
        'edit_group'   => ['id', 'title'],
        'create_navi'  => ['pid', 'title', 'data_type', 'content'],
        'edit_navi'    => ['id', 'title', 'data_type', 'content'],
    ];

    protected function checkContent($value, $rule, $data)
    {
        if ($data['data_type'] == 0 && $value == '') {
            return '导航内容不能为空';
        }

        if ($data['data_type'] == 1 && !$this->is($value, 'url')) {
            return '请填写正确的链接';
        }
        if ($data['data_type'] == 2 && $value <= 0) {
            return '请选择协议';
        }

        return true;
    }

}
